package dti;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import common.Constants;
import common.WordHistogram;

public class Vocabulary {
  private Map fileWordMaps;
  private Map masterVocab;
  
  public Vocabulary() {
    fileWordMaps = new HashMap();
  }
  
  public void addWordMap(String fileName, Map wordMap) {
    fileWordMaps.put(fileName, wordMap);
  }
  
  /**
   * Creates a master vocab list counting term occurrences across files.
   */
  public void createMasterList() {
    masterVocab = new HashMap();
    Iterator itrWordMaps = fileWordMaps.keySet().iterator();
    while(itrWordMaps.hasNext()) {
      Object fileName = itrWordMaps.next();
      Map wordMap = (Map)fileWordMaps.get(fileName);
      Iterator itrWords = wordMap.keySet().iterator();
      while(itrWords.hasNext()) {
        String word = itrWords.next().toString();
        Integer count = (Integer)wordMap.get(word);
        
        if(masterVocab.containsKey(word)) {
          Integer masterCount = (Integer) masterVocab.get(word);
          int newCount = count.intValue() + masterCount.intValue();
          masterCount = new Integer(newCount);
          masterVocab.put(word, masterCount);
        }
        else
          masterVocab.put(word, count);
      }      
    }
    
  }
  
  /**
   * Prunes the master vocabulary list to produce the set of terms that 
   * should be indexed for each file, where applicable.
   */
  public void pruneVocabList(int threshold, double upperBound, double lowerBound, Integer maxResults) {
    masterVocab = WordHistogram.trim(masterVocab, threshold, upperBound, lowerBound, maxResults);
    
    //  whichever terms are removed from the master list should also be removed from the word map for each file
    Iterator itrWordMaps = fileWordMaps.keySet().iterator();
    while(itrWordMaps.hasNext()) {
      Object fileName = itrWordMaps.next();
      Map wordMap = (Map)fileWordMaps.get(fileName);
      Iterator itrWords = wordMap.keySet().iterator();
      while(itrWords.hasNext()) {
        String word = itrWords.next().toString();
        if(!masterVocab.containsKey(word))
          itrWords.remove();
      }
    }
  }
  
  public String getTermString(String filePath) {
    StringBuffer sb = new StringBuffer();
    Map wordMap = (Map)fileWordMaps.get(filePath);
    Iterator itrWords = wordMap.keySet().iterator();
    while(itrWords.hasNext()) {
      String word = itrWords.next().toString();
      Integer count = (Integer)wordMap.get(word);
      sb.append(word).append(Constants.TERM_SEPARATOR)
        .append(count).append(Constants.TERM_SEPARATOR);
    }
    return sb.toString();
  }
  

}
